#!/usr/bin/env bash
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Wrapper for genearing PyCharm-compatible virtualenv.
set -e

HERE=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)
BUILDROOT=$HERE/../..
VENV_DIR=build-support/python/pycharm.venv
PROJECT_NAME=${PWD##*/}

pushd "$BUILDROOT"
  ./pants dependencies --external-only src/test/python:: | sort -u > requirements.txt
  ./pants setup-py api/src/main/thrift/org/apache/aurora/gen
  ./pants setup-py api/src/main/thrift/org/apache/thermos

  # Here we grab the version of pytest libs used by pants in a fairly awkward way.
  # TODO(John Sirois): Either get this info from the current pants install itself instead of
  # using a parallel ephemeral install like we do here (slow), or else invert things and
  # allow user-control of the pytest lib versions within reason.
  rm -rf "$VENV_DIR"
  ./build-support/virtualenv "$VENV_DIR"
  source $VENV_DIR/bin/activate
    python -m pip install pantsbuild.pants==$(./pants --version)
    pytest_requirement=$(python -m pip freeze | grep pytest==)
    pytest_cov_requirement=$(python -m pip freeze | grep pytest-cov==)
  deactivate

  rm -rf "$VENV_DIR"
  ./build-support/virtualenv "$VENV_DIR"
  source $VENV_DIR/bin/activate
    # NOTE: PyCharm navigation (ctrl-click) doesn't work without --egg - it gets confused by
    # the namespace packages in twitter.common.
    python -m pip install --egg -r requirements.txt
    python -m pip install --egg -f dist \
      apache.aurora.thrift==$(< .auroraversion) \
      apache.thermos.thrift==$(<.auroraversion)
    # Use same py.test version embedded into pants
    python -m pip install $pytest_requirement $pytest_cov_requirement
  deactivate
popd

if [ ! -d .idea ]; then
  echo "Creating PyCharm project at .idea/$PROJECT_NAME.iml"
  mkdir .idea
  cat <<"EOF" > .idea/$PROJECT_NAME.iml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
  <component name="NewModuleRootManager">
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/python" isTestSource="false" />
      <excludeFolder url="file://$MODULE_DIR$/.pants.d" />
      <excludeFolder url="file://$MODULE_DIR$/dist" />
      <excludeFolder url="file://$MODULE_DIR$/out" />
    </content>
    <orderEntry type="sourceFolder" forTests="false" />
  </component>
  <component name="TestRunnerService">
    <option name="projectConfiguration" value="py.test" />
    <option name="PROJECT_TEST_RUNNER" value="py.test" />
  </component>
</module>
EOF
fi

echo "Your PyCharm environment is now set up.  You can open the project root
directory with PyCharm.

Once the project is loaded:
  - open project settings
  - click 'Project Interpreter'
  - click the cog in the upper-right corner
  - click 'Add Local'
  - select '$VENV_DIR/bin/python'
  - click 'OK'

Once this is done, all imports should be recognized, and you can run unit tests in the IDE!"
